package cn.edu.sasu.adapter;

import java.io.File;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.edu.sasu.http.HttpRequestService;
import cn.edu.sasu.vo.News;
import cn.edu.sasu.R;

/**
 * 这个适配器是，所有的新闻的ListView使用的适配器，用户绑定新闻的条目
 * 
 * @author yubaolinfish
 * 
 */
@SuppressLint("HandlerLeak")
public class NewsItemAdapter extends BaseAdapter {

	private List<News> list;
	private LayoutInflater inflater;
	private News news;
	private Context context;

	public NewsItemAdapter(List<News> list, Context context) {

		this.context = context;
		this.list = list;
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.news_item, null);
			holder.iv_news_photo = (ImageView) convertView
					.findViewById(R.id.iv_news_photo);
			holder.tv_title = (TextView) convertView
					.findViewById(R.id.tv_title_news_top_item);
			holder.tv_Description = (TextView) convertView
					.findViewById(R.id.tv_description_news_top_item);
			holder.tv_count = (TextView) convertView
					.findViewById(R.id.tv_news_count_comments);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		news = list.get(position);
		holder.tv_title.setText(news.getNewsTitle());
		holder.tv_Description.setText(news.getNewsContent());

		String commtent = context.getResources().getString(R.string.comment);

		holder.tv_count.setText(news.getCommentsCount() + commtent);
		if (news.getPhotos() != null) {

			String photoName = news.getPhotos().get(0).getNewsFilmResourceUrl();

			String photoUrl = context.getResources().getString(R.string.android_url);// Android系统下的图片文件
			
			// 这里需要异步交互
			news.setPhoto(new File(photoUrl, photoName.split("/")[1]));
			// 从后台读取照片
			if (!news.getPhoto().exists()) {
				
				//	Log.i("Test", "哈哈，显示后台的图片复反反复复反反复复反反复复吩咐");
				//new HttpRequestService().putPhoto(photoName, context);
				new MyThread(photoName, holder,news.getPhoto()).start();// 开启线程去获取图片
			}else
				//Log.i("Test", "哈哈，显示Android系统下的的图片"+news.getPhoto().getPath());
				holder.iv_news_photo.setImageURI(Uri.fromFile(news.getPhoto()));
			
		} else {
			//	Log.i("Test", "哈哈，显示默认的图片");
			holder.iv_news_photo
					.setImageResource(R.drawable.news_item_default_photo);// 如果没有图片，那么就先系统默认的图片（灰色的文理）
		}

		return convertView;
	}

	private class MyThread extends Thread {
		private String photoName = "";
		private ViewHolder holder = null;
		private File file = null;

		private MyHandler handler = new MyHandler();

		public MyThread(String photoName, ViewHolder holder, File file) {
			this.photoName = photoName;
			this.holder = holder;
			this.file = file;
		}

		public void run() {

			new HttpRequestService().putPhoto(photoName, context);

			handler.setHolder(holder);
			handler.setFile(file);
			handler.sendEmptyMessage(1);
		};
	}

	private class MyHandler extends Handler {
		private File file = null;
		private ViewHolder holder = null;

		public void setHolder(ViewHolder holder) {
			this.holder = holder;
		}

		public void setFile(File file) {
			this.file = file;
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				holder.iv_news_photo.setImageURI(Uri.fromFile(file));
				break;

			}
		}
	}

	/**
	 * 新闻的一个内容
	 * 
	 * @author yubaolinfish
	 */
	public static class ViewHolder {
		protected TextView tv_title;
		protected TextView tv_Description;
		protected ImageView iv_news_photo;
		protected TextView tv_count;
	}

}
